package gui.renderers;

import gui.DBMS;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

import dto.DatabaseIconDTO;
import dto.TableIconDTO;

public class DBMSTreeCellRenderer implements TreeCellRenderer, MouseListener {
	private JLabel label;

	public DBMSTreeCellRenderer() {
		super();
		this.label = new JLabel();
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		Object obj = ((DefaultMutableTreeNode) value).getUserObject();
		if (obj instanceof DatabaseIconDTO) {
			ImageIcon dbicon = new ImageIcon(
					DBMS.class.getResource("./images/database-mini.png"));
			label.setIcon(dbicon);
			label.setText(((DatabaseIconDTO) obj).getDatabaseName());
		} else if (obj instanceof TableIconDTO) {
			ImageIcon tableicon = new ImageIcon(
					DBMS.class.getResource("./images/table-mini.png"));
			label.setIcon(tableicon);
			label.setText(((TableIconDTO) obj).getTableName());
		} else {
			
		}
		return label;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		label.setBackground(Color.BLUE);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger()) {
			label.setBackground(Color.BLUE);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger()) {
			label.setBackground(Color.BLUE);
		}
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
